package person.bing.fbcloud.leetcode;

import java.util.*;

/**
 * @author: mengfanbing
 * @date: 2020/8/4 10:19
 */
public class Q207 {
    public static void main(String[] args) {
        int[][] prerequisites = {{1,0}};
        boolean b = canFinish(2, prerequisites);
        System.out.println(b);
    }

    //超时
    public static boolean canFinish(int numCourses, int[][] prerequisites) {
        //看看有没有闭环，如果有,完成不了课程
        int length = prerequisites.length;
        for (int i=0;i<length;i++){
            int i1 = prerequisites[i][0];
            int i2 = prerequisites[i][1];
            Set<Integer> set = new HashSet<>();
            set.add(i1);
            boolean util = util(i2, length, prerequisites, set);
            if (util == false){
                return false;
            }
        }
        return true;
    }
    public static boolean util(int ele,int length,int[][] prerequisites,Set<Integer> set){
        for (int j=0;j<length;j++){
            int eleTemp = prerequisites[j][0];
            if (ele == eleTemp){
                int eleTemp2 = prerequisites[j][1];
                boolean add = set.add(eleTemp2);
                if (false == add){
                    return false;
                }
                return true && util(eleTemp2,length,prerequisites,set);
            }
        }
        return true;
    }
}
